package com.caine.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caine.common.entity.R;
import com.caine.order.entity.Goods;
import com.caine.order.entity.Order;
import com.caine.order.feign.IGoodFeignService;
import com.caine.order.feign.IPaymentFeignService;
import com.caine.order.mapper.OrderMapper;
import com.caine.order.service.IOrderService;
import com.caine.order.vo.OrderVO;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author   V   
 * @since 2021-01-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private IGoodFeignService goodFeignService;

    @Autowired
    private IPaymentFeignService paymentFeignService;

    /**
     * 创建订单
     *
     * @param vo
     * @return
     */
    @GlobalTransactional
    @Transactional(rollbackFor = {RuntimeException.class,Exception.class})
    @Override
    public R createOrder(OrderVO vo) {
        Goods goods = goodFeignService.getGoodsDetail(vo);
        System.out.println("stock = " + goods.getStock());
        vo.setPrice(goods.getPrice());
        boolean b = goodFeignService.reduceStock(vo);
        System.out.println("reduceStock = " + b);
        boolean pay = paymentFeignService.pay(vo);
        System.out.println("pay = " + pay);
        return R.success();
    }
}
